/*  File Name	: ServiceProcess.cpp
 *  Author		: Pang Jiacai
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-6-30
 *  Description:
 */

#include "ServiceProcess.h"
#include "Reactor.h"
#include "GateMonitor.h"
#include "EngineMonitor.h"
#include "ChatMonitor.h"
#include "SandBoxMonitor.h"
#include "DBMonitor.h"
#include "DirectoryMonitor.h"
#include "Configurator.h"
#include "Daemon.h"

ServiceProcess *ServiceProcess::instance_ =NULL;

ServiceProcess::ServiceProcess()
{
	this->line_ = 0;
	this->type_ = "";
	//this->reactor(Reactor::instance());
}

ServiceProcess::~ServiceProcess()
{

}

ServiceProcess *ServiceProcess::instance(void)
{
	if(NULL==instance_)
	{
		instance_ = new ServiceProcess();
	}
	return instance_;
}

int ServiceProcess::handle_signal(int signum)
{
	LOG((INFO_LOG,"ServiceProcess::handle_signal[%d]",signum));
	if(signum ==SIGNAL_GAME_STOP)
	{
		this->stop_service();
	}
	return 0;
}

int ServiceProcess::register_signal(int signum)
{
	return this->reactor()->register_signal(signum,this);
}

int ServiceProcess::start_service(int argc,char *argv[])
{
	LOG((INFO_LOG,"ServiceProcess::start_service[%s][%s][%s][%s]",
			argv[0],argv[1],argv[2],argv[3]))

	this->type_=argv[1];
	this->line_ =atoi(argv[2]);

	Logger::instance()->open(type_+"_"+argv[2]);

	LOG((INFO_LOG,"ServiceProcess::start_service[%s:%d]",type_.c_str(),line_));

	int ret = 0;
	if(this->type_==AUTH_SERVICE)
	{
		ret =this->start_auth(line_);
	}
	else if(this->type_==ENGINE_SERVICE)
	{
		ret =this->start_engine(line_);
	}
	else if(this->type_==GATE_SERVICE)
	{
		ret =this->start_gateway(line_);
	}
	else if(this->type_==CHAT_SERVICE)
	{
		ret =this->start_chat(line_);
	}
	else if(this->type_ == SANDBOX)
	{
		ret = this->start_sandbox();
	}
	else if(this->type_ == DBSERVER)
	{
		ret = this->start_dbserver(line_);
	}
	else if(this->type_== DIRCSERVER)
	{
		ret = this->start_dircserver(line_);
	}
	else
	{
		LOG((ERROR_LOG,"ServiceProcess::start_service unknown type:%s",this->type_.c_str()));
	}
	LOG((INFO_LOG,"main thread exit[%s:%d]",type_.c_str(),line_));
	return ret;
}

int ServiceProcess::start_auth(int line)
{
	return 0;
}

int ServiceProcess::start_gateway(int line)
{
	if(GateMonitor::instance()->init(line)!=0)
	{
		LOG((ERROR_LOG,"ServiceProcess::start_gateway-->init error"));
		return -1;
	}
	this->reactor(GateMonitor::instance()->reactor());
	this->register_signal(SIGNAL_GAME_STOP);
	return GateMonitor::instance()->start();
}

int ServiceProcess::start_engine(int line)
{
	if(EngineMonitor::instance()->init(line)!=0)
	{
		return -1;
	}
	this->reactor(EngineMonitor::instance()->reactor());
	this->register_signal(SIGNAL_GAME_STOP);
	return EngineMonitor::instance()->start();
}

int ServiceProcess::start_chat(int line)
{
	if(ChatMonitor::instance()->init(line)!=0)
	{
		return -1;
	}
	this->reactor(ChatMonitor::instance()->reactor());
	this->register_signal(SIGNAL_GAME_STOP);
	return ChatMonitor::instance()->start();
}

int ServiceProcess::start_sandbox(void)
{
	if(SandBoxMonitor::instance()->init(0)!=0)
	{
		return -1;
	}
	this->reactor(SandBoxMonitor::instance()->reactor());
	this->register_signal(SIGNAL_GAME_STOP);
	return SandBoxMonitor::instance()->start();
}

int ServiceProcess::start_dbserver(int line)
{
	if(DBMonitor::instance()->init(line)!=0)
	{
		return -1;
	}
	this->reactor(DBMonitor::instance()->reactor());
	this->register_signal(SIGNAL_GAME_STOP);
	return DBMonitor::instance()->start();
}

int ServiceProcess::start_dircserver(int line)
{
	if(DirectoryMonitor::instance()->init(line)!=0)
	{
		return -1;
	}
	this->reactor(DirectoryMonitor::instance()->reactor());
	this->register_signal(SIGNAL_GAME_STOP);
	return DirectoryMonitor::instance()->start();
}

int ServiceProcess::stop_service(void)
{
	if(this->type_ == GATE_SERVICE)
	{
		return GateMonitor::instance()->stop();
	}
	else if(this->type_ == ENGINE_SERVICE)
	{
		return EngineMonitor::instance()->stop();
	}
	else if(this->type_ == AUTH_SERVICE)
	{
		return 0;
	}
	else if(this->type_ == CHAT_SERVICE)
	{
		return ChatMonitor::instance()->stop();
	}
	else if(this->type_ == SANDBOX)
	{
		return SandBoxMonitor::instance()->stop();
	}
	else if(this->type_ == DBSERVER)
	{
		return DBMonitor::instance()->stop();
	}
	else if(this->type_==DIRCSERVER)
	{
		return DirectoryMonitor::instance()->stop();
	}
	else
	{
		LOG((ERROR_LOG,"ServiceProcess::stop_service unknown type:[%s]",this->type_.c_str()));
	}
	return 0;
}

